package com.web.pay;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.raizlabs.android.dbflow.sql.Query;
import com.raizlabs.android.dbflow.sql.language.OperatorGroup;
import com.raizlabs.android.dbflow.sql.language.SQLite;
import com.web.pay.db.UserPayDbModel;
import com.web.pay.db.UserPayDbModel_Table;

import java.util.ArrayList;
import java.util.List;

public class RecordActivity extends AppCompatActivity {
    private Button mBtnBack;
    private RecyclerView mRecyclerView;

    private ArrayList<UserPayDbModel> mUserPayList;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record);

        mUserPayList = new ArrayList<>();
        List<UserPayDbModel> payList = SQLite.select().from(UserPayDbModel.class).queryList();
        mUserPayList.clear();
        mUserPayList.addAll(payList);

        mBtnBack = findViewById(R.id.btn_back);
        mRecyclerView = findViewById(R.id.recycler);

        mBtnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(new MyAdapter());

        View view = findViewById(R.id.top);
        view.setBackgroundColor(Color.parseColor("#999999"));
    }


    private class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView mTextUserName;
        public TextView mTextMoney;
        public TextView mTextTime;
        public TextView mTextPayTime;
        public TextView mTextStatus;

        public MyViewHolder(View itemView) {
            super(itemView);

            mTextUserName = itemView.findViewById(R.id.user_name);
            mTextMoney = itemView.findViewById(R.id.money);
            mTextPayTime = itemView.findViewById(R.id.pay_type);
            mTextTime = itemView.findViewById(R.id.create_time);
            mTextStatus = itemView.findViewById(R.id.status);
        }
    }

    public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {

        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(RecordActivity.this).inflate(R.layout.layout_record_recycler_item
                    ,parent,false);
            return new MyViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
            UserPayDbModel payDbModel = mUserPayList.get(position);
            holder.mTextUserName.setText(payDbModel.user_name);
            holder.mTextMoney.setText(payDbModel.money);
            if(payDbModel.status == 0) {
                holder.mTextStatus.setTextColor(Color.RED);
                holder.mTextStatus.setText("未提交");
            }else if(payDbModel.status == 1){
                holder.mTextStatus.setTextColor(Color.GREEN);
                holder.mTextStatus.setText("已提交");
            }
            if(payDbModel.pay_type == 1) {
                holder.mTextPayTime.setText("微信");
            }else if(payDbModel.pay_type == 2){
                holder.mTextPayTime.setText("支付宝");
            }
            holder.mTextTime.setText(payDbModel.create_time);
        }

        @Override
        public int getItemCount() {
            return mUserPayList.size();
        }
    }
}
